package com.example.orderingfood.controller;

import com.example.orderingfood.entity.SubsoribeTable;
import com.example.orderingfood.service.SubsoribeTableService;
import com.example.orderingfood.vo.RestResult;
import com.example.orderingfood.vo.RestResultBuilder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * (SubsoribeTable)控制层
 *
 * @author makejava
 * @since 2021-03-24 16:12:54
 */
@RestController
@RequestMapping("/subsoribeTable")
public class SubsoribeTableController {
    /**
     * 服务对象
     */
    @Resource
    private SubsoribeTableService subsoribeTableService;

    /**
     * 通过主键查询单条数据
     *
     * @param subsoribeTable 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public RestResult selectOne(SubsoribeTable subsoribeTable) {
        SubsoribeTable result = subsoribeTableService.selectById(subsoribeTable.getId());
        if (result != null) {
            return new RestResultBuilder<>().success("查询成功", result);
        } else {
            return new RestResultBuilder<>().error("查询失败");
        }
    }

    /**
     * 新增一条数据
     *
     * @param subsoribeTable 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public RestResult insert(@RequestBody SubsoribeTable subsoribeTable) {
        int result = subsoribeTableService.insert(subsoribeTable);
        if (result > 0) {
            return new RestResultBuilder<>().success("新增成功", subsoribeTable);
        }
        return new RestResultBuilder<>().error("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param subsoribeTable 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public RestResult update(@RequestBody SubsoribeTable subsoribeTable) {
        SubsoribeTable result = subsoribeTableService.update(subsoribeTable);
        if (result != null) {
            return new RestResultBuilder<>().success("修改成功", result);
        }
        return new RestResultBuilder<>().error("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param subsoribeTable 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public RestResult delete(SubsoribeTable subsoribeTable) {
        int result = subsoribeTableService.deleteById(subsoribeTable.getId());
        if (result > 0) {
            return new RestResultBuilder<>().success("删除成功", null);
        }
        return new RestResultBuilder<>().error("删除失败");
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public RestResult selectAll() {
        List<SubsoribeTable> subsoribeTables = subsoribeTableService.selectAll();
        if (subsoribeTables != null) {
            return new RestResultBuilder<>().success("查询成功", subsoribeTables);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public RestResult selectPage(Integer start, Integer limit) {
        List<SubsoribeTable> subsoribeTables = subsoribeTableService.selectPage(start, limit);
        if (subsoribeTables != null) {
            return new RestResultBuilder<>().success("查询成功", subsoribeTables);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

}
